package cn.rui.controller;

import cn.rui.constant.MessageConstant;
import cn.rui.entity.Result;
import cn.rui.pojo.OrderSetting;
import cn.rui.service.OrderSettingService;
import cn.rui.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置
 *
 * @author 徽州大都督
 * @date 2020/8/26
 */
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    //上传Excel文件
    @PostMapping("upload")
    public Result upload(MultipartFile excelFile) {

        try {
            List<String[]> excel = POIUtils.readExcel (excelFile);  //使用POI工具类解析数据

            //处理数据
            List<OrderSetting> data = new ArrayList<> ();
            for (String[] strings : excel) {

                //判断如果少于2条数据就跳过
                if (strings.length<2) {
                    continue;
                } else {

                    if (!strings[0].contains ("/")||strings[0].equals ("")||StringUtils.isEmpty (strings[0])||strings[1].equals ("")||StringUtils.isEmpty (strings[1])){
                        continue;
                    }else {
                        //日期
                        String dataStr = strings[0];

                        //可预约数量
                        String numStr = strings[1];
                        OrderSetting setting = null;

                        Date date = null;
                        try {
                            date = new Date (dataStr);
                        } catch (Exception e) {
                            e.printStackTrace ();
                            //日期格式有误
                            return new Result (false,MessageConstant.DADE_ERROR);
                        }
                        setting = new OrderSetting (date, Integer.parseInt (numStr));

                        data.add (setting);
                    }


                }

            }

            //将数据保存到数据库
            orderSettingService.add (data);

            return new Result (true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace ();

            //文件解析失败
            return new Result (false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    //根据月份查询对应的预约信息
    @GetMapping("getOrderSettingByMonth/{date}")
    public Result getOrderSettingByMonth(@PathVariable("date")String date){ //date格式:2020-08

        try {
            List<Map<String,Object>> orderSettings=orderSettingService.getOrderSettingByMonth(date);
            return new Result (true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettings);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    //修改可预约人数
    @PostMapping("updateNumberByDate/{date}/{number}")
    @PreAuthorize("hasAnyAuthority('ORDERSETTING')")
    public Result updateNumberByDate(@PathVariable("date")String date,@PathVariable("number")Integer number) throws ParseException {

        //System.out.println (date);
        //System.out.println (number);


        try {
            orderSettingService.updateNumberByDate(date,number);

        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.ORDERSETTING_FAIL);
        }

        return new Result (true,MessageConstant.ORDERSETTING_SUCCESS);


    }
}
